"""
Copyright (c) 2022 Guangdong University of Technology
PhotLab is licensed under [Open Source License].
You can use this software according to the terms and conditions of the [Open Source License].
You may obtain a copy of [Open Source License] at: [https://open.source.license/]

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

See the [Open Source License] for more details.

Author: Meng Xiang, Junjiang Xiang
Created: 2023/8/19
Supported by: National Key Research and Development Program of China
"""
import numpy as np
import math


def iqm(input_1, input_2, input_optical_carrier, Extinction_ratio_db, Vpi, VDC):
    alpha = np.sqrt(1/10**(Extinction_ratio_db/10))         #  THE DIFFERENCE IN THE UPPER AND LOWER SPLITTER BRANCH
    signal_i = input_1
    signal_q = input_2

    mzm_Out_i = np.sqrt(0.5+alpha) * np.exp(1j * (signal_i/Vpi * math.pi+VDC/Vpi * math.pi)) + np.sqrt(0.5-alpha) * np.exp(-1j*(signal_i/Vpi * math.pi + VDC/Vpi * math.pi))
    mzm_Out_q = np.sqrt(0.5+alpha) * np.exp(1j * (signal_q/Vpi * math.pi+VDC/Vpi * math.pi)) + np.sqrt(0.5-alpha) * np.exp(-1j*(signal_q/Vpi * math.pi + VDC/Vpi * math.pi))

    output_optical=mzm_Out_i+mzm_Out_q*1j

    iqm_output=output_optical * input_optical_carrier
    return iqm_output


def iq_modulator(input, Optical_tx_laser, bias, Extinction_ratio_db, Vpi):
    """IQ 调制器
        Args:
            input[0]: 输入X偏振信号，numpy类型
            input[1]: 输入Y偏振信号，numpy类型
            Optical_tx_laser: 光载波信号
            Extinction_ratio_db: 消光比
            VPI：VPI
            bias：VDC、偏置电压

        Returns:
            output[0]: 输出X偏振信号
            output[1]: 输出Y偏振信号
        """
    input_1 = input[0]
    input_2 = input[1]

    Tx_Samples_X_mod = iqm(np.real(input_1), np.imag(input_1), Optical_tx_laser,
                                   Extinction_ratio_db, Vpi, bias)
    Tx_Samples_Y_mod = iqm(np.real(input_2), np.imag(input_2), Optical_tx_laser,
                                   Extinction_ratio_db, Vpi, bias)

    output = [Tx_Samples_X_mod, Tx_Samples_Y_mod]

    return output